package org.vsg.security.model.service.impl;

import java.io.Serializable;
import java.util.Collection;

import org.vsg.common.biz.exception.ServiceException;
import org.vsg.common.pagination.Pagination;
import org.vsg.common.pagination.PaginationException;
import org.vsg.common.pagination.carrier.Carrier;
import org.vsg.common.pagination.carrier.RecordLimitCarrier;
import org.vsg.common.pagination.datatrans.PaginationProvider;
import org.vsg.common.pagination.datatrans.PaginationProviderFactory;
import org.vsg.security.model.domain.SysMenuResource;
import org.vsg.security.model.domain.UrlResource;
import org.vsg.security.model.persistence.SysMenuResourceDao;
import org.vsg.security.model.persistence.UrlResourceDao;
import org.vsg.security.model.service.UrlResourceService;


public class UrlResourceServiceImpl implements UrlResourceService {
	/**
	 * set the pagination provider factory
	 * 
	 * 
	 */
	private PaginationProviderFactory factory = PaginationProviderFactory
			.getInstance();

	private UrlResourceDao urlResourceDao;

	private SysMenuResourceDao sysMenuResourceDao;	
	
	/**
	 * @param carrier
	 * @return
	 * @exception ServiceException
	 * @exception PaginationException
	 */
	public Pagination pageUrlResource(Carrier carrier) throws ServiceException,
			PaginationException {
		// TODO: implement
		PaginationProvider provider = factory
				.generateProvider((RecordLimitCarrier) carrier);
		return urlResourceDao.pageUrlResource(provider);
	}

	/**
	 * @param carrier
	 * @return
	 * @throws ServiceException
	 * @exception PaginationException
	 */
	public Pagination pageUrlResourceByAll(Carrier carrier) throws ServiceException,
			PaginationException {
		// TODO: implement
		PaginationProvider provider = factory
				.generateProvider((RecordLimitCarrier) carrier);
		return urlResourceDao.pageUrlResourceByAll(provider);
	}
	
	@Override
	public void saveAll(Collection<UrlResource> all)
			throws ServiceException {
		// TODO Auto-generated method stub
		urlResourceDao.saveAll(all);
		
	}  	

	/**
	 * @param urlResource
	 * @exception ServiceException
	 */
	public void addUrlResource(UrlResource urlResource) throws ServiceException {
		// TODO: implement
		urlResourceDao.save(urlResource);
	}

	/**
	 * @param urlResource
	 * @exception ServiceException
	 */
	public void updateUrlResource(UrlResource urlResource) throws ServiceException {
		// TODO: implement
		urlResourceDao.update(urlResource);
	}

	/**
	 * @param urlResource
	 * @exception ServiceException
	 */
	public void updateSimpleUrlResource(UrlResource newEntity) throws ServiceException {
		// TODO: implement
		UrlResource original = urlResourceDao.load( newEntity.getId() );
		
		original.setState( newEntity.getState()  );
		original.setFlagType( newEntity.getFlagType()  );
		
		// --- update the method ---
		urlResourceDao.update( original );		
		
	}

	/**
	 * @param urlResource
	 * @exception ServiceException
	 */
	public void deleteUrlResource(UrlResource urlResource) throws ServiceException {
		// TODO: implement
		urlResourceDao.delete(urlResource);
	}

	@Override
	public void deleteAll(Collection<UrlResource> all)
			throws ServiceException {
		// TODO Auto-generated method stub
		urlResourceDao.deleteAll(all);
	}
	

	/**
	 * @param pk
	 * @exception ServiceException
	 */
	public UrlResource loadUrlResource(Serializable pk) throws ServiceException {
		// TODO: implement
		return urlResourceDao.load(pk);
	}


	@Override
	public Collection<UrlResource> findByAll() throws ServiceException {
		// TODO Auto-generated method stub
		return urlResourceDao.findByAll();
	}


	public SysMenuResourceDao getSysMenuResourceDao() {
		return sysMenuResourceDao;
	}

	public void setSysMenuResourceDao(SysMenuResourceDao sysMenuResourceDao) {
		this.sysMenuResourceDao = sysMenuResourceDao;
	}

	public UrlResourceDao getUrlResourceDao() {
		return urlResourceDao;
	}

	/**
	 * @param newUrlResourceDao
	 */
	public void setUrlResourceDao(UrlResourceDao newUrlResourceDao) {
		urlResourceDao = newUrlResourceDao;
	}

	@Override
	public Collection<UrlResource> findUrlResourceByUrl(String url,
			String context) throws ServiceException {
		// TODO Auto-generated method stub
		return urlResourceDao.findUrlResourceByUrl(url, context);
	}

	@Override
	public Collection<String> findAllContext() throws ServiceException {
		// TODO Auto-generated method stub
		return urlResourceDao.findContextByAll();
	}

	@Override
	public Collection<UrlResource> findUrlResourcesByContext(String context)
			throws ServiceException {
		// TODO Auto-generated method stub
		return urlResourceDao.findUrlResourcesByContext(context);
	}

	@Override
	public void bindingMenuUrlResource(UrlResource urlResource,
			SysMenuResource menuResource) throws ServiceException {
		// TODO Auto-generated method stub
		UrlResource orgResource = this.urlResourceDao.load( urlResource.getId() );
		
		SysMenuResource orgMenuResource = this.sysMenuResourceDao.load( menuResource.getId() );
		
		orgMenuResource.setAssociationResource( orgResource );
		orgMenuResource.setFlagStrategy( (short)1 );
		
		this.sysMenuResourceDao.update( orgMenuResource );		
	}




}